<template>
  <div class="app-container">
    <wise-crud ref="wise-crud" :urls="urls" :fields="fields" :filters="filters" :handle="handle" />
  </div>
</template>

<script>
import WiseCrud from '@/components/Crud/wiseCrud'
import {
  postHandle
} from '@/api'
export default {
  name: 'WarehouseManage',
  components: {
    WiseCrud
  },
  data() {
    return {
      urls: {
        list: '/api/v1/welder-sbtx-second/monitor-unit',
        add: `/api/v1/welder-sbtx-second/monitor-unit-comb/create1  `,
        show: '/api/v1/welder-sbtx-second/monitor-unit/show/',
        update: '/api/v1/welder-sbtx-second/monitor-unit-comb/update1/',
        delete: '/api/v1/welder-sbtx-second/monitor-unit-comb/remove1/'
      },
      fields: [{
        label: '单位名称',
        prop: 'name',
        type: 'text',
        use: ['table', 'view', 'edit', 'add'],
        placeholder: '请输入单位名称',
        rules: [{
          required: true,
          message: '必填项不能为空',
          trigger: 'blur'
        }]
      },
      {
        label: '单位地址',
        type: 'area',
        use: ['edit', 'add'],
        town: false,
        objKeys: [{
          field: 'province',
          key: 'province',
          value: 'key'
        },
        {
          field: 'provinceName',
          key: 'province',
          value: 'value'
        },
        {
          field: 'city',
          key: 'city',
          value: 'key'
        },
        {
          field: 'cityName',
          key: 'city',
          value: 'value'
        },
        {
          field: 'country',
          key: 'area',
          value: 'key'
        },
        {
          field: 'countryName',
          key: 'area',
          value: 'value'
        }
        ],
        rules: [{
          required: false,
          message: '必填项不能为空',
          trigger: 'blur'
        }]
      },
      {
        label: '单位地址',
        type: 'render',
        use: ['table', 'view'],
        tableRender: (h, row, index) => {
          return h('div', this.areaHandle(row))
        },
        viewRender: (h, item, object) => {
          return h('div', this.areaHandle(object))
        }
      },
      {
        label: '公司LOGO',
        prop: 'address',
        type: 'file',
        use: ['table', 'view', 'edit', 'add'],
        placeholder: '公司LOGO',
        rules: [{
          required: false,
          message: '请选个人头像',
          trigger: 'blur'
        }]
      },
      {
        label: '姓名',
        prop: 'username',
        type: 'text',
        use: ['table', 'view', 'edit', 'add'],
        placeholder: '请输入姓名',
        rules: [{
          required: true,
          message: '必填项不能为空',
          trigger: 'blur'
        }]
      },
      {
        label: '账号',
        prop: 'phone',
        type: 'text',
        use: ['table', 'view', 'edit', 'add'],
        placeholder: '请输入账号',
        rules: [{
          required: true,
          message: '必填项不能为空',
          trigger: 'blur'
        },
        {
          pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/,
          message: '格式有误',
          trigger: 'blur'
        }
        ]
      },
      {
        label: '操作',
        type: 'handle',
        use: ['table'],
        handle: ['view', 'edit', 'delete'],
        tableWidth: 260
      }
      ],
      total: 0
    }
  },
  computed: {
    handle() {
      return ['add']
      if (this.total === 0) {
        return ['add']
      } else {
        return []
      }
    },
    spaceId() {
      return this.$store.getters.spaceId
    },
    filters() {
      return [{
        field: 'spaceId',
        operator: 'EQUALS',
        value: this.spaceId
      },
      {
        field: 'type',
        operator: 'EQUALS',
        value: 1
      }
      ]
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs['wise-crud'].$refs['wiseTable'].$watch('ts', ts => {
        this.total = ts
      })
    })
  },
  methods: {
    areaHandle({
      provinceName,
      cityName,
      countryName
    }) {
      if (provinceName && cityName && countryName) {
        return provinceName + cityName + countryName
      } else if (provinceName && cityName) {
        return provinceName + cityName
      } else if (provinceName) {
        return provinceName
      } else {
        return
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
